package com.xh.filepicker.sdk;

import android.util.Log;

import androidx.annotation.StyleRes;

import com.xh.filepicker.sdk.models.Config;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by Abhishek Tiwari on 09-01-2021.
 */
public final class ConfigBuilder {
    private String rootDir;
    private boolean showHidden = false;
    private boolean selectMultiple = false;
    private int selectMaxNum;
    private boolean addDivider = false;
    private boolean showOnlyDir = false;

    @StyleRes
    private int themeId = 0; //R.style.UnicornFilePicker_Default

    private final XHFilePicker unicornFilePicker;
    private ArrayList<String> extensionFilters;
    private Config config;

    public ConfigBuilder(XHFilePicker unicornFilePicker) {
        this.unicornFilePicker = unicornFilePicker;
        this.config = Config.getCleanInstance();
    }

    public ConfigBuilder setRootDirectory(String dirPath){
        this.rootDir = dirPath;
        Log.d("FilePickerSDK", "setRootDirectory() " + dirPath);
        return this;
    }

    public ConfigBuilder showHiddenFiles(boolean value){
        this.showHidden = value;
        Log.d("FilePickerSDK", "showHiddenFiles() " + value);
        return this;
    }

    public ConfigBuilder selectMultipleFiles(boolean value){
        this.selectMultiple = value;
        Log.d("FilePickerSDK", "selectMultipleFiles() " + value);
        return this;
    }

    public ConfigBuilder selectMaxNum(int selectMaxNum){
        this.selectMaxNum = selectMaxNum;
        Log.d("FilePickerSDK", "selectMaxNum() " + selectMaxNum);
        return this;
    }

    public ConfigBuilder setFilters(String[] filters){
        if (filters != null) {
            this.extensionFilters = new ArrayList<>(Arrays.asList(filters));
            Log.d("FilePickerSDK", "setFilters() " + filters.toString());
        }
        return this;
    }

    public ConfigBuilder addItemDivider(boolean value){
        this.addDivider = value;
        Log.d("FilePickerSDK", "addItemDivider() " + value);
        return this;
    }

//    public ConfigBuilder theme(@StyleRes int theme){
//        this.themeId = theme;
//        return this;
//    }

    public ConfigBuilder showOnlyDirectory(boolean value){
        this.showOnlyDir = value;
        Log.d("FilePickerSDK", "showOnlyDirectory() " + value);
        return this;
    }

    public XHFilePicker build(){
        config.setRootDir(this.rootDir);
        config.setSelectMultiple(this.selectMultiple);
        config.setSelectMaxNum(selectMaxNum);
        config.setShowHidden(this.showHidden);
        config.setExtensionFilters(this.extensionFilters);
        config.setAddItemDivider(this.addDivider);
        config.setThemeId(this.themeId);
        config.setShowOnlyDirectory(this.showOnlyDir);
        if (showOnlyDir) {
            config.setSelectMultiple(false);
        }
        return unicornFilePicker;
    }


}
